package org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.entity.RespBean;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.entity.User;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.service.IUserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: Ben
 * @Date: 2021/6/19 16:45
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "用户接口",value = "UserController")
public class UserController {

    protected static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    IUserService iUserService;

    @PostMapping(value = "/user/insertUser")
    @ApiOperation(value = "增加用户信息",httpMethod = "POST")
    public RespBean insert(@Valid User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("插入失败！");
            for (FieldError error : errors) {
                logger.error("错误的字段名：" + error.getField());
                logger.error("错误信息：" + error.getDefaultMessage());
                map.put(error.getField(), error.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        boolean judgment = iUserService.insert(user);
        if (judgment){
            logger.info("添加成功");
            return RespBean.ok("添加成功");
        }else {
            logger.error("该用户已存在，添加失败");
            return RespBean.error("该用户已存在，添加失败");
        }
    }

    @DeleteMapping(value = "/user/deleteUser")
    @ApiOperation(value = "删除用户信息",httpMethod = "DELETE")
    public RespBean deleteMember(Integer userId) {
        if (iUserService.delete(userId)) {
            logger.info("删除用户成功");
            return RespBean.ok("删除用户成功");
        } else {
            logger.info("删除用户失败");
            return RespBean.error("删除用户失败");
        }
    }

    @PutMapping(value = "/user/updateUser")
    @ApiOperation(value = "更改项目信息")
    public RespBean update(@Valid User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("插入失败！");
            for (FieldError error : errors) {
                logger.error("错误的字段名：" + error.getField());
                logger.error("错误信息：" + error.getDefaultMessage());
                map.put(error.getField(), error.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        boolean judgment = iUserService.update(user);
        if (judgment){
            logger.info("更改成功");
            return RespBean.ok("更改成功");
        }else {
            logger.error("该用户不存在，更改失败");
            return RespBean.error("该用户不存在，更改失败");
        }
    }
    @GetMapping(value = "/user/selectUserByPage")
    @ApiOperation(value = "分页查询用户信息")
    public RespBean selectUserByPage(Integer pageNumber, Integer pageSize){
        if (pageNumber!=null && pageSize!=null){
            IPage<User> userIPage = iUserService.selectUserByPage(pageNumber, pageSize);
            List<User> users = userIPage.getRecords();
            if (users!=null){
                logger.info("分页查询成功");
                return RespBean.ok("分页查询成功",users);
            }else{
                logger.error("分页查询失败");
                return RespBean.error("分页查询失败");
            }
        }else {
            logger.error("分页查询失败");
            return RespBean.error("分页查询失败");
        }
    }


    @GetMapping(value = "/user/selectAll")
    @ApiOperation(value = "查询所有用户信息")
    public RespBean selectAll(){
        List<User> userList = iUserService.selectAll();
        return RespBean.ok("查询所有用户成功",userList);
    }

    @GetMapping("/user/select")
    @ApiOperation(value = "根据id查询用户信息")
  public RespBean select(Integer id){
        User user = iUserService.select(id);
        if (user != null) {
            logger.info("查询用户成功");
            return RespBean.ok("查询用户成功",user);
        } else {
            logger.info("查询用户失败");
            return RespBean.error("查询用户失败");
        }
  }
}